package cn.supermaster.management.web.controller.control;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import cn.supermaster.management.web.domain.UserTableModel;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.fasterxml.jackson.annotation.JsonFormat;
import cn.supermaster.management.common.controller.BaseController;
import cn.supermaster.management.common.page.PageDomain;
import cn.supermaster.management.common.page.TableDataInfo;
import cn.supermaster.management.common.page.TableSupport;
import cn.supermaster.management.common.utils.DateUtils;
import cn.supermaster.management.common.utils.TextUtils;

/**
 * All rights Reserved, Designed By www.super-bytecode.com
 *
 * @ProjectName: supermaster-management
 * @Package: cn.supermaster.management.web.controller.control
 * @ClassName: TableController
 * @Description: [表格相关]
 * <p> </p>
 * @Author: [Lance Ting]
 * @Date: 2021/1/16 15:00
 * @Version: V1.0
 * @Copyright: 2021 www.super-bytecode.comn Inc. All rights reserved.
 * TODO: 注意,本文件Lance Ting所作,如果转载或使用请标明具体出处!
 **/
@Controller
@RequestMapping("/control/table")
public class TableController extends BaseController {
    private String prefix = "control/table";

    private final static List<UserTableModel> users = new ArrayList<UserTableModel>();

    {
        users.add(new UserTableModel(1, "1000001", "测试1", "0", "15888888888", "ry@qq.com", 150.0, "0"));
        users.add(new UserTableModel(2, "1000002", "测试2", "1", "15666666666", "ry@qq.com", 180.0, "1"));
        users.add(new UserTableModel(3, "1000003", "测试3", "0", "15666666666", "ry@qq.com", 110.0, "1"));
        users.add(new UserTableModel(4, "1000004", "测试4", "1", "15666666666", "ry@qq.com", 220.0, "1"));
    }

    /**
     * @Description: [搜索相关]
     * <p></P>
     * @Title: skipSearchHtml
     * @Author: Lance Ting
     * @Date: 2021-01-16
     * @Return: java.lang.String
     */
    @GetMapping("/search")
    public String skipSearchHtml() {
        return prefix + "/search";
    }

    /**
     * @Description: [数据汇总]
     * <p></P>
     * @Title: skipFooterHtml
     * @Author: Lance Ting
     * @Date: 2021-01-16
     * @Return: java.lang.String
     */
    @GetMapping("/footer")
    public String skipFooterHtml() {
        return prefix + "/footer";
    }

    /**
     * @Description: [组合表头]
     * <p></P>
     * @Title: skipGroupHeaderHtml
     * @Author: Lance Ting
     * @Date: 2021-01-16
     * @Return: java.lang.String
     */
    @GetMapping("/groupHeader")
    public String skipGroupHeaderHtml() {
        return prefix + "/groupHeader";
    }

    /**
     * @Description: [表格导出]
     * <p></P>
     * @Title: skipExportHtml
     * @Author: Lance Ting
     * @Date: 2021-01-16
     * @Return: java.lang.String
     */
    @GetMapping("/export")
    public String skipExportHtml() {
        return prefix + "/export";
    }

    /**
     * @Description: [翻页记住选择]
     * <p></P>
     * @Title: skipRememberHtml
     * @Author: Lance Ting
     * @Date: 2021-01-16
     * @Return: java.lang.String
     */
    @GetMapping("/remember")
    public String skipRememberHtml() {
        return prefix + "/remember";
    }

    /**
     * @Description: [跳转至指定页]
     * <p></P>
     * @Title: skipPageGoHtml
     * @Author: Lance Ting
     * @Date: 2021-01-16
     * @Return: java.lang.String
     */
    @GetMapping("/pageGo")
    public String skipPageGoHtml() {
        return prefix + "/pageGo";
    }

    /**
     * @Description: [自定义查询参数]
     * <p></P>
     * @Title: skipParamsHtml
     * @Author: Lance Ting
     * @Date: 2021-01-16
     * @Return: java.lang.String
     */
    @GetMapping("/params")
    public String skipParamsHtml() {
        return prefix + "/params";
    }

    /**
     * @Description: [多表格]
     * <p></P>
     * @Title: skipMultiHtml
     * @Author: Lance Ting
     * @Date: 2021-01-16
     * @Return: java.lang.String
     */
    @GetMapping("/multi")
    public String skipMultiHtml() {
        return prefix + "/multi";
    }

    /**
     * @Description: [点击按钮加载表格]
     * <p></P>
     * @Title: skipButtonHtml
     * @Author: Lance Ting
     * @Date: 2021-01-16
     * @Return: java.lang.String
     */
    @GetMapping("/button")
    public String skipButtonHtml() {
        return prefix + "/button";
    }

    /**
     * @Description: [直接加载表格数据]
     * <p></P>
     * @Title: skipDataHtml
     * @Author: Lance Ting
     * @Date: 2021-01-16
     * @Param: modelMap
     * @Return: java.lang.String
     */
    @GetMapping("/data")
    public String skipDataHtml(ModelMap modelMap) {
        modelMap.put("users", users);
        return prefix + "/data";
    }

    /**
     * @Description: [表格冻结列]
     * <p></P>
     * @Title: skipFixedColumnsHtml
     * @Author: Lance Ting
     * @Date: 2021-01-16
     * @Return: java.lang.String
     */
    @GetMapping("/fixedColumns")
    public String skipFixedColumnsHtml() {
        return prefix + "/fixedColumns";
    }

    /**
     * @Description: [自定义触发事件]
     * <p></P>
     * @Title: skipEventHtml
     * @Author: Lance Ting
     * @Date: 2021-01-16
     * @Return: java.lang.String
     */
    @GetMapping("/event")
    public String skipEventHtml() {
        return prefix + "/event";
    }

    /**
     * @Description: [表格细节视图]
     * <p></P>
     * @Title: skipDetailHtml
     * @Author: Lance Ting
     * @Date: 2021-01-16
     * @Return: java.lang.String
     */
    @GetMapping("/detail")
    public String skipDetailHtml() {
        return prefix + "/detail";
    }

    /**
     * @Description: [表格父子视图]
     * <p></P>
     * @Title: skipChildHtml
     * @Author: Lance Ting
     * @Date: 2021-01-16
     * @Return: java.lang.String
     */
    @GetMapping("/child")
    public String skipChildHtml() {
        return prefix + "/child";
    }

    /**
     * @Description: [表格图片预览]
     * <p></P>
     * @Title: skipImageHtml
     * @Author: Lance Ting
     * @Date: 2021-01-16
     * @Return: java.lang.String
     */
    @GetMapping("/image")
    public String skipImageHtml() {
        return prefix + "/image";
    }

    /**
     * @Description: [动态增删改查]
     * <p></P>
     * @Title: skipCurdHtml
     * @Author: Lance Ting
     * @Date: 2021-01-16
     * @Return: java.lang.String
     */
    @GetMapping("/curd")
    public String skipCurdHtml() {
        return prefix + "/curd";
    }

    /**
     * @Description: [表格拖拽操作]
     * <p></P>
     * @Title: skipReorderHtml
     * @Author: Lance Ting
     * @Date: 2021-01-16
     * @Return: java.lang.String
     */
    @GetMapping("/reorder")
    public String skipReorderHtml() {
        return prefix + "/reorder";
    }

    /**
     * @Description: [表格列宽拖动]
     * <p></P>
     * @Title: skipResizableHtml
     * @Author: Lance Ting
     * @Date: 2021-01-16
     * @Return: java.lang.String
     */
    @GetMapping("/resizable")
    public String skipResizableHtml() {
        return prefix + "/resizable";
    }

    /**
     * @Description: [表格行内编辑操作]
     * <p></P>
     * @Title: skipEditableHtml
     * @Author: Lance Ting
     * @Date: 2021-01-16
     * @Return: java.lang.String
     */
    @GetMapping("/editable")
    public String skipEditableHtml() {
        return prefix + "/editable";
    }

    /**
     * @Description: [主子表提交]
     * <p></P>
     * @Title: skipSubdataHtml
     * @Author: Lance Ting
     * @Date: 2021-01-16
     * @Return: java.lang.String
     */
    @GetMapping("/subdata")
    public String skipSubdataHtml() {
        return prefix + "/subdata";
    }

    /**
     * @Description: [表格自动刷新]
     * <p></P>
     * @Title: skipRefreshHtml
     * @Author: Lance Ting
     * @Date: 2021-01-16
     * @Return: java.lang.String
     */
    @GetMapping("/refresh")
    public String skipRefreshHtml() {
        return prefix + "/refresh";
    }

    /**
     * @Description: [表格打印配置]
     * <p></P>
     * @Title: skipPrintHtml
     * @Author: Lance Ting
     * @Date: 2021-01-16
     * @Return: java.lang.String
     * @GetMapping("/print") public String skipPrintHtml() {
     * return prefix + "/print";
     * }
     * <p>
     * /**
     * @Description: [表格标题格式化]
     * <p></P>
     * @Title: skipHeaderStyleHtml
     * @Author: Lance Ting
     * @Date: 2021-01-16
     * @Return: java.lang.String
     */
    @GetMapping("/headerStyle")
    public String skipHeaderStyleHtml() {
        return prefix + "/headerStyle";
    }

    /**
     * @Description: [表格其他操作]
     * <p></P>
     * @Title: skipOtherHtml
     * @Author: Lance Ting
     * @Date: 2021-01-16
     * @Return: java.lang.String
     */
    @GetMapping("/other")
    public String skipOtherHtml() {
        return prefix + "/other";
    }


    /**
     * @Description: [查询数据]
     * <p></P>
     * @Title: queryList
     * @Author: Lance Ting
     * @Date: 2021-01-16
     * @Param: userModel
     * @Return: cn.supermaster.management.common.page.TableDataInfo
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo queryList(UserTableModel userModel) {
        TableDataInfo rspData = new TableDataInfo();
        List<UserTableModel> userList = new ArrayList<UserTableModel>(Arrays.asList(new UserTableModel[users.size()]));
        Collections.copy(userList, users);
        // 查询条件过滤
        if (TextUtils.isNotEmpty(userModel.getUserName())) {
            userList.clear();
            for (UserTableModel user : users) {
                if (user.getUserName().equals(userModel.getUserName())) {
                    userList.add(user);
                }
            }
        }
        PageDomain pageDomain = TableSupport.buildPageRequest();
        if (null == pageDomain.getPageNum() || null == pageDomain.getPageSize()) {
            rspData.setRows(userList);
            rspData.setTotal(userList.size());
            return rspData;
        }
        Integer pageNum = (pageDomain.getPageNum() - 1) * 10;
        Integer pageSize = pageDomain.getPageNum() * 10;
        if (pageSize > userList.size()) {
            pageSize = userList.size();
        }
        rspData.setRows(userList.subList(pageNum, pageSize));
        rspData.setTotal(userList.size());
        return rspData;
    }
}